/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.cvut.fel.taskpro.data.manager;

import cz.cvut.fel.taskpro.data.entity.SystemEnviroment;
import cz.cvut.fel.taskpro.data.imanager.IEnviromentData;
import java.io.Serializable;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 * This class implement #{@link IEnviromentData} interface
 * @author Tom
 * @version 1.0. beta
 */
@Stateless
public class EnviromentData extends ObjectManager implements IEnviromentData, Serializable {

    @Override
    public List<SystemEnviroment> getAllEnviroments() {
        CriteriaBuilder cb=em.getCriteriaBuilder();
        CriteriaQuery<SystemEnviroment> systemEnvQ=cb.createQuery(SystemEnviroment.class);
        Root<SystemEnviroment> rootSystemEnvq=systemEnvQ.from(SystemEnviroment.class);
        systemEnvQ.orderBy(cb.asc(rootSystemEnvq.get("keyOfSetting")));
        TypedQuery typedQuery=em.createQuery(systemEnvQ);
        List<SystemEnviroment> resultList=typedQuery.getResultList();
        return resultList;
    }

    @Override
    public void addSystemEnviroment(SystemEnviroment newEnviroment) {
        super.add(newEnviroment);
    }

    @Override
    public List<SystemEnviroment> getEnviroment(String key) {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<SystemEnviroment> envQuery = cb.createQuery(SystemEnviroment.class);
        Root<SystemEnviroment> rootEnvQuery = envQuery.from(SystemEnviroment.class);
        Predicate keyPredicate = cb.equal(rootEnvQuery.get("keyOfSetting"), key);
        envQuery.distinct(true).where(keyPredicate).orderBy(cb.asc(rootEnvQuery.get("keyOfSetting")));
        TypedQuery typedQuery = em.createQuery(envQuery);
        List<SystemEnviroment> resultList = typedQuery.getResultList();
        return resultList;
    }

    @Override
    public void update(SystemEnviroment enviromentToUpdate) {
     super.update(enviromentToUpdate);
    }

    @Override
    public void remove(SystemEnviroment enviromentToRemove) {
      super.remove(enviromentToRemove);
    }
}
